/*=============================================================================

  Library: HMPBundleFrame

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/


#include "ConfigurationEvent.h"

#include <Exception.h>

class ConfigurationEventData : public QSharedData
{
public:

  ConfigurationEventData(const IServiceReference& reference,
                            ConfigurationEvent::Type type,
                            const QString& factoryPid,
                            const QString& pid)
    : type(type), factoryPid(factoryPid), pid(pid), reference(reference)
  {

  }

  ConfigurationEventData(const ConfigurationEventData& other)
    : QSharedData(other), type(other.type), factoryPid(other.factoryPid),
      pid(other.pid), reference(other.reference)
  {

  }

  /**
   * Type of this event.
   */
  const ConfigurationEvent::Type type;

  /**
   * The factory pid associated with this event.
   */
  const QString factoryPid;

  /**
   * The pid associated with this event.
   */
  const QString pid;

  /**
   * The ConfigurationAdmin service which created this event.
   */
  const IServiceReference reference;
};

//----------------------------------------------------------------------------
ConfigurationEvent::ConfigurationEvent()
  : d(0)
{

}

//----------------------------------------------------------------------------
ConfigurationEvent::~ConfigurationEvent()
{

}

//----------------------------------------------------------------------------
bool ConfigurationEvent::isNull() const
{
  return !d;
}

//----------------------------------------------------------------------------
ConfigurationEvent::ConfigurationEvent(const IServiceReference& reference,
                      Type type, const QString& factoryPid,
                      const QString& pid)
  : d(new ConfigurationEventData(reference, type, factoryPid, pid))
{
  if (pid.isNull())
  {
    throw InvalidArgumentException("pid must not be null");
  }
}

//----------------------------------------------------------------------------
ConfigurationEvent::ConfigurationEvent(const ConfigurationEvent& other)
  : d(other.d)
{

}

//----------------------------------------------------------------------------
ConfigurationEvent& ConfigurationEvent::operator=(const ConfigurationEvent& other)
{
  d = other.d;
  return *this;
}

//----------------------------------------------------------------------------
QString ConfigurationEvent::getFactoryPid() const
{
  return d->factoryPid;
}

//----------------------------------------------------------------------------
QString ConfigurationEvent::getPid() const
{
  return d->pid;
}

//----------------------------------------------------------------------------
int ConfigurationEvent::getType() const
{
  return d->type;
}

//----------------------------------------------------------------------------
IServiceReference ConfigurationEvent::getReference() const
{
  return d->reference;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, ConfigurationEvent::Type type)
{
  switch (type)
  {
  case ConfigurationEvent::CM_UPDATED: return dbg << "CM_UPDATED";
  case ConfigurationEvent::CM_DELETED: return dbg << "CM_DELETED";

  default: return dbg << "unknown configuration event type (" << static_cast<int>(type) << ")";
  }
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const ConfigurationEvent& event)
{
  if (event.isNull()) return dbg << "NONE";

  dbg.nospace() << event.getType() << (event.getFactoryPid().isEmpty() ? " " : event.getFactoryPid() + ", ")
                   << "pid=" << event.getPid();
  return dbg.maybeSpace();
}
